package com.cyc.basic.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyc.basic.mapper.SysUserMapper;
import com.cyc.basic.pojo.entiy.SysUser;
import com.cyc.basic.pojo.vo.UserReq;
import com.cyc.basic.pojo.vo.UserSaveReq;
import com.cyc.basic.service.user.ISysUserService;
import com.cyc.common.enums.UserTypeEnum;
import com.cyc.common.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author chenyunchang
 * @since 2021-01-07
 */
@Service
@Slf4j
//@Validated
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TransactionTemplate transactionTemplate;

    @Override
    public void caseWhenTest(UserReq userReq) {
        List<SysUser> sysUsers = sysUserMapper.caseWhenTest(userReq);
        log.info(String.valueOf(sysUsers));
    }

    @Override
    public List<SysUser> dateTest(UserReq userReq) {
        List<String> dateTimeList = userReq.getDateTimeList();
        String beginTime = dateTimeList.get(0);
        String endTime = dateTimeList.get(1);
        LocalDateTime startDateWithHms = DateUtil.getStartDateWithHMS(beginTime);
        LocalDateTime endDateWithHms = DateUtil.getStartDateWithHMS(endTime);
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().between(SysUser::getCreateTime, startDateWithHms, endDateWithHms);
        return sysUserMapper.selectList(queryWrapper);
    }

    @Override
    public List<SysUser> testFilter() {
        List<SysUser> sysUserList = this.list();

        sysUserList = sysUserList.stream().filter(sysUser -> sysUser.getAge().equals(2)
                || sysUser.getId().equals(3)).collect(Collectors.toList());
        log.info("{}", sysUserList);
        return sysUserList;
    }

    @Override
    public List<SysUser> testQueryWrapper() {
        QueryWrapper<SysUser> queryWrapper1 = new QueryWrapper<>();
        //查询年龄为2的
        queryWrapper1.lambda().eq(SysUser::getAge, 2);
        //查询年龄为2或者名字是老六的
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().and(
                wrapper1 -> wrapper1.eq(SysUser::getAge, 2)
                        .or(wrapper2 -> wrapper2.eq(SysUser::getName, "老六")));
        List<SysUser> list = this.list(queryWrapper);
        log.info("{}", list);
        return list;
    }

    @Override
    public void batchInsertTest() {
        List<SysUser> sysUserList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            SysUser sysUser = new SysUser();
            sysUser.setId(null);
            sysUser.setName("张三" + i);
            sysUser.setAge(i);
            sysUser.setUserType(UserTypeEnum.IMAGE);
            sysUserList.add(sysUser);
        }
        this.saveBatch(sysUserList);

    }

    @Override
    public void saveUser(UserSaveReq userSaveReq) {
        log.info("校验通过");
    }

    @Override
    public void saveUserCheckByService(@Valid UserSaveReq userSaveReq) {
        log.info("校验通过");
    }

    @Override
    public void testEnum(UserReq userReq) {
        List<SysUser> sysUserList = sysUserMapper.testEnum(userReq);
    }

    @Override
//    @Transactional
    public void updateByIdTest() {
        transactionTemplate.execute(transactionStatus -> {
            SysUser sysUser = new SysUser();
            sysUser.setId(1);
            sysUser.setName("调用方");
            this.updateById(sysUser);
            this.updateTransaction();
            return true;
        });
    }


    //    @Transactional
    public void updateTransaction() {
        SysUser sysUser = new SysUser();
        sysUser.setId(2);
        sysUser.setName("测试spring事务");
        this.updateById(sysUser);
        int i = 1 / 0;
//        rocketMQTemplate.syncSend();
    }
}
